<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>socket.io</title>
    <link href="index.css" rel="stylesheet">
</head>

<body>
    <h3>socket.io</h3>
    <button id="send">发送消息到服务器</button>
    <div>
        <p>服务器返回的消息是：</p>
        <i id="msg"></i>
    </div>
    <script src="socket.io.js"></script>
    <script>
        var socket = io("ws://localhost:3000"); //初始化websocket，连接服务端
        var send = document.querySelector("#send"),
			msg = document.querySelector("#msg");
        send.onclick = function () {
            console.log("点击了发送消息！");
            socket.emit("send", "hello")
        }
        socket.on("getMsg", function (data) {
            console.log("服务端发送的消息是：", data);
            msg.innerHTML += data + '<br/>';
        })
    </script>
</body>

</html>
